#include "GlobalHead.h"
#include "CommandDraftTools.h"
#include "App/DocumentObject.h"
#include "Gui_Application.h"
#include "App_Application.h"
#include "App/Document.h"
#include "../AVIRecordQthread.h"//avi thread
#include "CustomPlayer.h"
#include "View3DInventor.h"
#include "MainWindow.h"
#include "VtRender.h"

AVIRecordQthread AVIRecord;

using namespace App;
using namespace Gui;
ToolsPathCamera::ToolsPathCamera()
:ToolsCommand("PathCamera")//,mTm(tm)
{
	sGroup        = QT_TR_NOOP("Drafting");
	sMenuText     = QT_TR_NOOP("PathCamera");
	sToolTipText  = QT_TR_NOOP("Camera Movement follow the path");
	sWhatsThis    = "PathCamera";
	sStatusTip    = QT_TR_NOOP("Move with path...");
	sPixmap       = "PathCamera";
}
void ToolsPathCamera::activated(int iMsg)
{
	DocumentObject* pObject = GetApp_Application().getActiveDocument()->addObject("Part::PartPathCamera","pathCamera");
	pObject->setAsImport();
	Command::doCommand((CommandStringFunction)App::Document::recomputeEntity);
	pObject->finshImport();
	activateTools("PathCamera",pObject);
}

ToolsFreeCamera::ToolsFreeCamera()
:ToolsCommand("FreeCamera")
{
	sGroup        = QT_TR_NOOP("Drafting");
	sMenuText     = QT_TR_NOOP("FreeCamera");
	sToolTipText  = QT_TR_NOOP("Camera Movement freely");
	sWhatsThis    = "Draft_Clone";
	sStatusTip    = QT_TR_NOOP("Move freely...");
	sPixmap       = "FreeCamera";
}
void ToolsFreeCamera::activated(int iMsg)
{
	DocumentObject* pFreeCamera = GetApp_Application().getActiveDocument()->addObject("Part::PartFreeCamera","freeCamera");
	pFreeCamera->setAsImport();
	Command::doCommand((CommandStringFunction)App::Document::recomputeEntity);
	pFreeCamera->finshImport();
	activateTools("FreeCamera",pFreeCamera);
}

ToolsMacroRecord::ToolsMacroRecord()
:ToolsCommand("macro-record")
{
	sGroup        = QT_TR_NOOP("Macro");
	sMenuText     = QT_TR_NOOP("macroRecord");
	sToolTipText  = QT_TR_NOOP("macroRecorder");
	sWhatsThis    = "RecordScreen";
	sStatusTip    = QT_TR_NOOP("Record...");
	sPixmap       = "macro-record";
}

void ToolsMacroRecord::activated(int iMsg)
{
	QString dir = QFileDialog::getSaveFileName(NULL, QDialog::tr("Save As"),QDialog::tr("*.avi"),QDialog::tr("*.avi"));
    if (dir.isEmpty())
    {
		return;
    }
	View3DInventor* view = qobject_cast<View3DInventor*>(getMainWindow()->activeWindow());
	if(!view) return;
	QPoint po = view->getVtRender()->mapToGlobal(QPoint(0,0));
	QRegion regi = view->getVtRender()->childrenRegion();
	AVIRecord.setRectName(po.x(),po.y(),view->getVtRender()->width(),view->getVtRender()->height(),dir);
	AVIRecord.recordstate = 1;
	Sleep(1000);
	AVIRecord.start();
	
}

ToolsRecordExcute ::ToolsRecordExcute()
:ToolsCommand("macro-execute")
{
	sGroup        = QT_TR_NOOP("Macro");
	sMenuText     = QT_TR_NOOP("executeRecord");
	sToolTipText  = QT_TR_NOOP("execute Record");
	sWhatsThis    = "executeRecordScreen";
	sStatusTip    = QT_TR_NOOP("execute...");
	sPixmap       = "macro-execute";
}

void ToolsRecordExcute::activated(int iMsg)
{
	QStringList sl;
	sl.append(AVIRecord.filenamepath);
	QString str = QString::fromAscii("Player.exe");
	if(!QProcess::startDetached(str,sl))
		QMessageBox::critical(0,QObject::tr("error"),QObject::tr("start Plyer.exe failed"));
}

ToolsRecordStop::ToolsRecordStop()
:ToolsCommand("macro-stop")
{
	sGroup        = QT_TR_NOOP("Macro");
	sMenuText     = QT_TR_NOOP("stopRecord");
	sToolTipText  = QT_TR_NOOP("stop Record");
	sWhatsThis    = "stopeRecordScreen";
	sStatusTip    = QT_TR_NOOP("stop...");
	sPixmap       = "macro-stop";
}

void ToolsRecordStop::activated(int iMsg)
{
    AVIRecord.recordstate = 0;
}

void CreateToolsExCommands()
{
	CommandManager &rcCmdMgr = Gui_Application::instance->commandManager();
	rcCmdMgr.addCommand(new ToolsPathCamera());
	rcCmdMgr.addCommand(new ToolsFreeCamera());
	rcCmdMgr.addCommand(new ToolsMacroRecord());
	rcCmdMgr.addCommand(new ToolsRecordExcute());
	rcCmdMgr.addCommand(new ToolsRecordStop());
}
